package com.yq.utils;


import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author 3307235355@qq.com
 * @description
 * @since 2022/06/30 16:38
 */
public class NetUtils {
    //静态方法 获取发送的数据报包
    public static DatagramPacket getSendPacket(String msg,String ip,int port) throws UnknownHostException {
        //数据的封装
        byte[] bytes = msg.getBytes();
        InetAddress targetIp = InetAddress.getByName(ip);
        DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length, targetIp, port);


        //返回一个包
        return  datagramPacket;
    }

    //静态方法 获取接收的数据报包

    public static DatagramPacket getReceivePacket(){

        byte[] bytes = new byte[1024];
        DatagramPacket receivePacket = new DatagramPacket(bytes,0,bytes.length);


        return  receivePacket;
    }
    //静态方法 获取解析的数据报包
    public static String parse(DatagramPacket packet){
    //
        byte[] data = packet.getData();
        int offset = packet.getOffset();
        int length = packet.getLength();
        String s = new String(data, offset, length);
        return s;

    }


}
